package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class WeChatServiceImpl implements WechatService {

    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    @Value("${wechat.appid}")
    private String APP_ID;

    @Value("${wechat.secret}")
    private String APP_SECRET;

    /***
     * 获取Open_ID
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        //https://api.weixin.qq.com/sns/jscode2session
        Map<String, Object> map = getAppConfig();
        map.put("appid", APP_ID);
        map.put("secret", APP_SECRET);
        map.put("js_code", code);
        String response = HttpUtil.createRequest(Method.GET, REQUEST_URL).form(map).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(response);
        return jsonObject.getStr("openid");
    }

    /***
     * 获取手机号
     * @param phoneCode
     * @return
     */
    @Override
    public String getPhone(String phoneCode) {
        String token = getAccessToken();
        String url = PHONE_REQUEST_URL+token;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code",phoneCode);
        //发起请求
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        //是一个map
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //判断接口响应是否出错
        if(jsonObject.getInt("errcode") != 0){
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }

        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    /***
     * 获取access_token
     * @return
     */
    public String getAccessToken() {
        //https://api.weixin.qq.com/cgi-bin/token
        Map<String, Object> map = getAppConfig();
        map.put("grant_type", "client_credential");
        map.put("appid", APP_ID);
        map.put("secret", APP_SECRET);
        String response = HttpUtil.createRequest(Method.GET, TOKEN_URL).form(map).execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(response);
        return jsonObject.getStr("access_token");
    }

    /**
     * 封装公共参数
     * @return
     */
    private Map<String, Object> getAppConfig() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid",APP_ID);
        paramMap.put("secret",APP_SECRET);
        return paramMap;
    }
}
